<?php

namespace app\zmt\controller;

use app\zmt\model\UserModel;
use app\zmt\validate\UserValidate;
use cmf\controller\AdminBaseController;

class UserController extends AdminBaseController
{
    public function index()
    {
        // 获取搜索关键字
        $keyword = input('get.keyword', '');
        $where = [];
        if ($keyword) {
            $where[] = ['name', 'like', '%' . $keyword . '%'];
        }

        // 查询所有用户并传递给视图
        $userList = UserModel::where($where)
            ->order("list_order", 'desc')
            ->paginate(3);

        $page = $userList->render();

        $this->assign('userList', $userList);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function addpost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $validate = new UserValidate();
            if (!$validate->scene('addpost')->check($data)) {
                $this->error($validate->getError());
            }

            // 确保 `switch` 字段有默认值
            $data['switch'] = isset($data['switch']) ? $data['switch'] : 1;

            $user = new UserModel();
            $user->name = $data['name'];
            $user->image = $data['image'];
            $user->mobile = $data['mobile'];
            $user->switch = $data['switch'];
            $user->create_time = date('Y-m-d H:i:s');

            if ($user->save()) {
                $this->success('添加成功', url('user/index'));
            } else {
                $this->error('添加失败');
            }
        }
    }

    public function changeStatus()
    {
        $id = input('param.id', 0, 'intval');
        $user = UserModel::find($id);

        if ($user) {
            // 切换状态
            $user->switch = $user->switch == 1 ? 0 : 1;

            if ($user->save()) {
                $this->success('状态切换成功', 'user/index');
            } else {
                $this->error('状态切换失败', 'user/index');
            }
        } else {
            $this->error('用户不存在', 'user/index');
        }
    }

    public function edit($id)
    {
        $user = UserModel::find($id);
        if (!$user) {
            $this->error('用户不存在');
        }

        $this->assign('user', $user);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $validate = new UserValidate();
            if (!$validate->scene('editPost')->check($data)) {
                $this->error($validate->getError());
            }

            $user = UserModel::find($data['id']);
            if (!$user) {
                $this->error('用户不存在');
            }

            // switch 的默认值
            $data['switch'] = isset($data['switch']) ? $data['switch'] : 1;

            $user->name = $data['name'];
            $user->image = $data['image'];
            $user->mobile = $data['mobile'];
            $user->switch = $data['switch'];
            $user->update_time = date('Y-m-d H:i:s');

            if ($user->save()) {
                $this->success('编辑成功', url('user/index'));
            } else {
                $this->error('编辑失败');
            }
        }
    }

    public function delete($id)
    {
        $user = UserModel::find($id);
        if (!$user) {
            $this->error('用户不存在');
        }

        if ($user->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function order()
    {
        parent::listOrders(new UserModel());
        $this->success('排序成功');
    }
}
